////////////////////////////////////////////////////////////////////////////////
//File:					UglyOpenGlError.cpp
//Author:				Kesitem
//This file is part of UglyProject.
//See UglyProject.txt for copyright informations.
////////////////////////////////////////////////////////////////////////////////
#include "UglyOpenGlError.h"
#include "UglyLog.h"

namespace Ugly
{

/**
 */
OpenGlError &gOpenGlError = OpenGlError::instance();


/**
 * \brief Get an unique instance of the class.
 *
 * \return Class instance.
 */
OpenGlError &OpenGlError::instance()
{
	static OpenGlError openGlError;

	return(openGlError);
}


/**
 */
OpenGlError::OpenGlError()
{
    mErrorDescription = "";
}


/**
 */
OpenGlError::~OpenGlError()
{
}

/**
 * \brief Perform a quick check on a OpenGL call. Write text and error in log.
 *
 * \param   _Text   Text to write in log if error.
 */
void OpenGlError::quickCheck(const std::string &_Text)
{
    if(this->check())
    {
        gLog << "OpenGL error! " << _Text << "\n";
        gLog << "   error: " << this->getErrorDesc() << "\n";
    }
}


/**
 */
bool OpenGlError::check()
{
    GLenum glEnum = glGetError();

    if(glEnum == GL_NO_ERROR)
    {
        mErrorDescription = "GL_NO_ERROR: No error has been recorded. The value of this symbolic constant is guaranteed to be 0.";
        return false;
    }

    switch(glEnum)
	{
	case GL_INVALID_ENUM:
		mErrorDescription = "GL_INVALID_ENUM: An unacceptable value is specified for an enumerated argument. The offending command is ignored and has no other side effect than to set the error flag.";
	break;

	case GL_INVALID_VALUE:
		mErrorDescription = "GL_INVALID_VALUE: A numeric argument is out of range. The offending command is ignored and has no other side effect than to set the error flag.";
	break;

	case GL_INVALID_OPERATION:
		mErrorDescription = "GL_INVALID_OPERATION: The specified operation is not allowed in the current state. The offending command is ignored and has no other side effect than to set the error flag.";
	break;

	case GL_OUT_OF_MEMORY:
		mErrorDescription = "GL_OUT_OF_MEMORY: There is not enough memory left to execute the command. The state of the GL is undefined, except for the state of the error flags, after this error is recorded.";
	break;

	default:
		mErrorDescription = "Unknow error.";
	break;
	}

    return true;
}


/**
 */
std::string OpenGlError::getErrorDesc()
{
    return mErrorDescription;
}

}//namespace Ugly

